package com.example.config;

import com.example.entity.User;
import org.apache.log4j.Logger;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by Administrator on 2017/3/2 0002.
 */
@Configuration
@EnableAutoConfiguration
public class RedisConfig {

    //以下的语句会覆盖RedisConnectionFactory的信息，RedisConnectionFactory是读取application.properties
//    @Bean
//    JedisConnectionFactory jedisConnectionFactory() {
//        return new JedisConnectionFactory();
//    }
    @Bean
    public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, User> template = new RedisTemplate<String, User>();
//        template.setConnectionFactory(jedisConnectionFactory());//这句话是错误的，默认是localhost本地连接
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new RedisObjectSerializer());
        return template;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate();
//        template.setConnectionFactory(jedisConnectionFactory());//这句话是错误的，默认是localhost本地连接
        template.setConnectionFactory(factory);
//        template.setKeySerializer(new StringRedisSerializer());//不需要序列化
//        template.setValueSerializer(new RedisObjectSerializer());//不需要序列化
        return template;
    }

}
